﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ConstantCMapEditor3
{
    public class LightChangeButton : Scene
    {
        LightSourceComponent target;

        public LightChangeButton(LightSourceComponent ChangeTarget, int X, int Y)
        {
            DeclarationDeocrator.AnimateLight.Touchable = true;

            DeclarationDeocrator.AnimateLight.TouchEvent += DeclarationDeocrator.AnimateLightChange;
            DeclarationDeocrator.AnimateLight.Text = "[ Animate ]";
            DeclarationDeocrator.AnimateLight.SpriteColor = Color.Blue;
            DeclarationDeocrator.AnimateLight.Position = new Vector2(0, 60);

            this.AttachChild(DeclarationDeocrator.AnimateLight);

            target = ChangeTarget;
            this.Position = new Vector2(X, Y);
            this.Touchable = true;




            TextSprite Range = new TextSprite();
            Range.Touchable = true;
            Range.Text = "[ Range ]";
            Range.TouchEvent += RangeClick;
            Range.SpriteColor = Color.Black;

            this.AttachChild(Range);


            TextSprite Intensity = new TextSprite();
            Intensity.Touchable = true;
            Intensity.Text = "[ Intensity ]";
            Intensity.TouchEvent += IntensityClick;
            Intensity.SpriteColor = Color.Blue;

            this.AttachChild(Intensity);

            TextSprite Shadow = new TextSprite();
            Shadow.Touchable = true;
            Shadow.Text = "[ Shadow ]";
            Shadow.TouchEvent += ShadowClick;
            Shadow.SpriteColor = Color.Blue;

            this.AttachChild(Shadow);


            Range.Position = new Vector2(-75, 0);
            Intensity.Position = new Vector2(50, 0);
            Shadow.Position = new Vector2(-25, 25);
        }

        private void RangeClick(MouseEvent e, Scene s)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
            {
                if (e.WheelUp)
                {
                    target.Size += new Vector2(1, 0);
                }
                else if (e.WheelDown)
                {
                    target.Size -= new Vector2(1, 0);
                }
            }
            else
            {
                if (e.WheelUp)
                {
                    target.Size += new Vector2(10, 0);
                }
                else if (e.WheelDown)
                {
                    target.Size -= new Vector2(10, 0);
                }
            }
        }

        private void IntensityClick(MouseEvent e, Scene s)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
            {
                if (e.WheelUp)
                {
                    target.Intensity += 0.01f;
                }
                else if (e.WheelDown)
                {
                    target.Intensity -= 0.01f;
                }
            }
            else
            {
                if (e.WheelUp)
                {
                    target.Intensity += 0.1f;
                }
                else if (e.WheelDown)
                {
                    target.Intensity -= 0.1f;
                }
            }
        }

        private void ShadowClick(MouseEvent e, Scene s)
        {
            {
                if (e.ClickDownEdgeL)
                {
                    target.Shadow++;
                    target.Shadow %= 8;

                    Console.WriteLine(target.Shadow);
                }
            }
        }
    }

    public static class DeclarationDeocrator
    {
        public static TextSprite Layer1 ;
        public static TextSprite Layer2 ;
        public static TextSprite Layer3 ;
        public static TextSprite Layer4 ;
        public static TextSprite Layer5 ;
        public static TextSprite ActivateL1;
        public static TextSprite ActivateL2;
        public static TextSprite ActivateL3;
        public static TextSprite ActivateL4;
        public static TextSprite ActivateL5;
        public static TextSprite Batch;
        public static TextSprite AnimateLight = new TextSprite();
        public static TextSprite WorkSpaceLock;



        static public Scene AttachLayerEvent(Scene target,int difX,int difY) 
        {
            Scene root = new Scene();
            root.Position = new Vector2(difX, difY);
            root.Touchable = true;
            Layer1 = new TextSprite();
            Layer2 = new TextSprite();
            Layer3 = new TextSprite();
            Layer4 = new TextSprite();
            Layer5 = new TextSprite();
            ActivateL1 = new TextSprite();
            ActivateL2 = new TextSprite();
            ActivateL3 = new TextSprite();
            ActivateL4 = new TextSprite();
            ActivateL5 = new TextSprite();
            Batch = new TextSprite();
            WorkSpaceLock = new TextSprite();

            /*  DeclarationDeocrator.AnimateLight.Touchable =*/
            WorkSpaceLock.Touchable = Batch.Touchable = ActivateL1.Touchable = ActivateL2.Touchable = ActivateL3.Touchable = ActivateL4.Touchable = ActivateL5.Touchable = Layer1.Touchable = Layer2.Touchable = Layer3.Touchable = Layer4.Touchable = Layer5.Touchable = root.Touchable = true;
            target.AttachChild(root);
            root.AttachChild(Layer1);
            root.AttachChild(Layer2);
            root.AttachChild(Layer3);
            root.AttachChild(Layer4);
            root.AttachChild(Layer5);
            root.AttachChild(ActivateL1);
            root.AttachChild(ActivateL2);
            root.AttachChild(ActivateL3);
            root.AttachChild(ActivateL4);
            root.AttachChild(ActivateL5);
            root.AttachChild(Batch);
            root.AttachChild(WorkSpaceLock);
           // root.AttachChild(AnimateLight);

            Layer1.TouchEvent += Layer1Selected;
            Layer2.TouchEvent += Layer2Selected;
            Layer3.TouchEvent += Layer3Selected;
            Layer4.TouchEvent += Layer4Selected;
            Layer5.TouchEvent += Layer5Selected;
            WorkSpaceLock.TouchEvent += WorkSpaceLockCLick;

            Batch.TouchEvent += BatchChange;
            Batch.Text = "[ Batch ]";
      //      AnimateLight.TouchEvent += AnimateLightChange;
      //      AnimateLight.Text = "[ Light ]";

            ActivateL1.TouchEvent += View1Selected;
            ActivateL2.TouchEvent += View2Selected;
            ActivateL3.TouchEvent += View3Selected;
            ActivateL4.TouchEvent += View4Selected;
            ActivateL5.TouchEvent += View5Selected;


            Layer1.Text = "[ L1 ]";
            Layer2.Text = "[ L2 ]";
            Layer3.Text = "[ L3 ]";
            Layer4.Text = "[ L4 ]";
            Layer5.Text = "[ L5 ]";
            ActivateL1.Text = "[ " + true + " ]";
            ActivateL2.Text = "[ " + true + " ]";
            ActivateL3.Text = "[ " + true + " ]";
            ActivateL4.Text = "[ " + true + " ]";
            ActivateL5.Text = "[ " + true + " ]";
            WorkSpaceLock.Text = "[ WorkSpaceLock:"+Game1.WorkSpaceLock+"]";

            int difYY = 50;

            Layer1.Position = new Vector2(-800, 50 + difYY);
            Layer2.Position = new Vector2(-700, 50 + difYY);
            Layer3.Position = new Vector2(-600, 50 + difYY);
            Layer4.Position = new Vector2(-500, 50 + difYY);
            Layer5.Position = new Vector2(-400, 50 + difYY);

            ActivateL1.Position = new Vector2(-800, 70 + difYY);
            ActivateL2.Position = new Vector2(-700, 70 + difYY);
            ActivateL3.Position = new Vector2(-600, 70 + difYY);
            ActivateL4.Position = new Vector2(-500, 70 + difYY);
            ActivateL5.Position = new Vector2(-400, 70 + difYY);

            Batch.Position = new Vector2(50, 30);
       //     AnimateLight.Position = new Vector2(150,30);
            WorkSpaceLock.Position = new Vector2(80, 60);


            WorkSpaceLock.SpriteColor = Batch.SpriteColor /*= AnimateLight.SpriteColor*/ = Color.Blue;
            ActivateL1.SpriteColor = ActivateL2.SpriteColor = ActivateL3.SpriteColor = ActivateL4.SpriteColor = ActivateL5.SpriteColor = Layer1.SpriteColor = Layer2.SpriteColor = Layer3.SpriteColor = Layer4.SpriteColor = Layer5.SpriteColor = Color.Gray;
            Layer2.SpriteColor = Color.White;

            return root;
        }

        static public BatchChangeForm batchForm = new BatchChangeForm();
        static public AnimatedLightForm animatedLightForm = new AnimatedLightForm();

        static private void WorkSpaceLockCLick(MouseEvent e, Scene s) 
        {
            if (e.ClickDownEdgeL)
            {
                Game1.WorkSpaceLock = !Game1.WorkSpaceLock;
                WorkSpaceLock.Text = "[ WorkSpaceLock:" + Game1.WorkSpaceLock + "]";
            }
        }

        static private void BatchChange(MouseEvent e, Scene s) 
        {
            if (e.ClickDownEdgeL)
            {
                try
                {
                    batchForm.Show();
                }
                catch 
                {
                    batchForm = new BatchChangeForm();
                    
                    
                    batchForm.Show();
                }
            }
        }


        static public void AnimateLightChange(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                try
                {
                    animatedLightForm.Show();
                }
                catch
                {
                    animatedLightForm = new AnimatedLightForm();


                    animatedLightForm.Show();

                  //  Game1.Selected = Game1.Selected;
                }
            }
        }
        

        static private void Layer1Selected(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL) 
            {
                Game1.SpriteEditorScene.LayerEditState = LayerState.Layer1;
                Layer1.SpriteColor = Layer2.SpriteColor = Layer3.SpriteColor = Layer4.SpriteColor = Layer5.SpriteColor = Color.Gray;
                Layer1.SpriteColor = Color.White;


                while (!Game1.SpriteEditorScene.ChangeView(1)) ;
            }
        }
        static private void Layer2Selected(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                Game1.SpriteEditorScene.LayerEditState = LayerState.Layer2;
                Layer1.SpriteColor = Layer2.SpriteColor = Layer3.SpriteColor = Layer4.SpriteColor = Layer5.SpriteColor = Color.Gray;
                Layer2.SpriteColor = Color.White;

                while (!Game1.SpriteEditorScene.ChangeView(2)) ;
            }
        }

        static private void Layer3Selected(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                Game1.SpriteEditorScene.LayerEditState = LayerState.Layer3;
                Layer1.SpriteColor = Layer2.SpriteColor = Layer3.SpriteColor = Layer4.SpriteColor = Layer5.SpriteColor = Color.Gray;
                Layer3.SpriteColor = Color.White;

                while (!Game1.SpriteEditorScene.ChangeView(3)) ;
            }
        }

        static private void Layer4Selected(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                Game1.SpriteEditorScene.LayerEditState = LayerState.Layer4;
                Layer1.SpriteColor = Layer2.SpriteColor = Layer3.SpriteColor = Layer4.SpriteColor = Layer5.SpriteColor = Color.Gray;
                Layer4.SpriteColor = Color.White;

                while (!Game1.SpriteEditorScene.ChangeView(4)) ;
            }
        }


        static private void Layer5Selected(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                Game1.SpriteEditorScene.LayerEditState = LayerState.Layer5;
                Layer1.SpriteColor = Layer2.SpriteColor = Layer3.SpriteColor = Layer4.SpriteColor = Layer5.SpriteColor = Color.Gray;
                Layer5.SpriteColor = Color.White;

                while (!Game1.SpriteEditorScene.ChangeView(5)) ;
            }
        }

        static private void View1Selected(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                ActivateL1.Text = "[ " + Game1.SpriteEditorScene.ChangeView(1) + " ]";
            }
        }

        static private void View2Selected(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                ActivateL2.Text = "[ " + Game1.SpriteEditorScene.ChangeView(2) + " ]";
            }
        }

        static private void View3Selected(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                ActivateL3.Text = "[ " + Game1.SpriteEditorScene.ChangeView(3) + " ]";
            }
        }

        static private void View4Selected(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                ActivateL4.Text = "[ " + Game1.SpriteEditorScene.ChangeView(4) + " ]";
            }
        }

        static private void View5Selected(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                ActivateL5.Text = "[ " + Game1.SpriteEditorScene.ChangeView(5) + " ]";
            }
        }

        static public Scene ChangeColorButton(Scene parent,Scene scene, int difX, int difY) 
        {
            Scene temp = new ColorChangeButton(scene, difX, difY);
            parent.AttachChild(temp);
            return temp;
        }

        static public Scene SetLightChangeButton(Scene parent, LightSourceComponent scene, int difX, int difY) 
        {
            Scene temp = new LightChangeButton(scene, difX, difY);
            parent.AttachChild(temp);
            return temp;
        }

        static public Scene ChangeRenameButton(Scene parent,Declaration d, int difX, int difY) 
        {
            Scene temp = new RenameButton(d, difX, difY);
            parent.AttachChild(temp);
            return temp;
        }

        static public Scene ChangeModeButton(Scene scene,int difX,int difY) 
        {
            Scene root = new Scene();
            root.Touchable = true;
            root.Position = new Vector2(difX, difY);

            TextSprite MapMode = new TextSprite();
            MapMode.Text = "[Map]";
            MapMode.SpriteColor = Color.Blue;
            MapMode.Touchable = true;
            MapMode.TouchEvent += clickMapMode;
            root.AttachChild(MapMode);

            TextSprite SpriteMode = new TextSprite();
            SpriteMode.Text = "[Sprite]";
            SpriteMode.SpriteColor = Color.Blue;
            SpriteMode.Touchable = true;
            SpriteMode.TouchEvent += clickSpriteMode;
            root.AttachChild(SpriteMode);

            MapMode.Position = new Vector2(-40, 0);
            SpriteMode.Position = new Vector2(40, 0);

            scene.AttachChild(root);

            return root;
        }

        static private void clickMapMode(MouseEvent e, Scene s) 
        {
            if(e.ClickDownEdgeL)
            {
            Game1.ChangeStateToMapMode();
            }
        }

        static private void clickSpriteMode(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                Game1.ChangeStateToSpriteMode();
            }
        }

        static public Scene SetFourRotationButton(Declaration targetItem,Scene targetScene,int difX,int difY)
        {
            Scene root = new Scene();
            root.Position = new Vector2(difX, difY);
            root.Touchable = true;
            targetScene.AttachChild(root);

            TextSprite b1 = new RotationButton(targetItem, 0);
            TextSprite b2 = new RotationButton(targetItem, (float)(Math.PI / 2));
            b2.Position = b1.Position + new Vector2(0,b1.Height);
            TextSprite b3 = new RotationButton(targetItem, (float)(Math.PI * 2 / 2));
            b3.Position = b2.Position + new Vector2(0, b2.Height);
            TextSprite b4 = new RotationButton(targetItem, (float)(Math.PI * 3 / 2));
            b4.Position = b3.Position + new Vector2(0, b3.Height);

            root.AttachChild(b1);
            root.AttachChild(b2);
            root.AttachChild(b3);
            root.AttachChild(b4);

            return root;
        }

        static public Scene SetFouDirectionSelectButton(Declaration targetItem, Scene targetScene, int difX, int difY)
        {
            Scene root = new Scene();
            root.Position = new Vector2(difX, difY);
            root.Touchable = true;
            targetScene.AttachChild(root);

            TextSprite b1 = new DirectionSelectButton(targetItem, "Direction.Down");
            TextSprite b2 = new DirectionSelectButton(targetItem, "Direction.Up");
            b2.Position = b1.Position + new Vector2(0, b1.Height);
            TextSprite b3 = new DirectionSelectButton(targetItem, "Direction.Right");
            b3.Position = b2.Position + new Vector2(0, b2.Height);
            TextSprite b4 = new DirectionSelectButton(targetItem, "Direction.Left");
            b4.Position = b3.Position + new Vector2(0, b3.Height);

            root.AttachChild(b1);
            root.AttachChild(b2);
            root.AttachChild(b3);
            root.AttachChild(b4);

            return root;
        }

        static public Scene SetLockAt32Button(Scene targetScene,NormalSprite targetSprite,int difX,int difY)
        {
            Scene root = new Scene();
            root.Touchable = true;
            targetScene.AttachChild(root);
            root.Position = new Vector2(difX, difY);

            LockAt32Button button = new LockAt32Button(targetSprite);
            root.AttachChild(button);

            return root;
        }

    }

    public class LockAt32Button : TextSprite
    {
        NormalSprite _target;
        public LockAt32Button(NormalSprite sprite) 
        {
            _target = sprite;

            this.Touchable = true;
            this.Text = "[ LockAt32 ]";
            this.SpriteColor = Color.Blue;
            this.TouchEvent += Click;
        }

        private void Click(MouseEvent e, Scene s) 
        {
            if (e.ClickDownEdgeL) 
            {
                if (_target.LockAt32)
                {
                    Command c = new SpriteUnLockCommand(_target);
                    Game1.commandManager.Execute(c);
                }
                else 
                {
                    Command c = new SpriteLockCommand(_target);
                    Game1.commandManager.Execute(c);
                }
            }
        }
    }

    public class RotationButton : TextSprite
    {
        Declaration TargetItem;
        float TargetRotate;

        public RotationButton(Declaration targetItem, float rotate) : base()
        {
            TargetItem = targetItem;
            Touchable = true;
            TouchEvent += Click;
            TargetRotate = rotate;
            Text = "[ Rotate = " + TargetRotate + " ]";
            SpriteColor = Color.Blue;
        }

        private void Click(MouseEvent e,Scene s)
        {
            if (e.ClickUpEdgeL)
            {
                Game1.commandManager.Execute(new RotateCommand(TargetItem, TargetRotate));
            }
        }
    }


    public class RenameButton : Scene 
    {
        Declaration target;

        public RenameButton(Declaration ChangeTarget, int X, int Y) 
        {
            target = ChangeTarget;
            this.Position = new Vector2(X, Y);
            this.Touchable = true;

            TextSprite Panel = new TextSprite();
            Panel.Touchable = true;
            Panel.Text = "[ Rename ]";
            Panel.TouchEvent += RenameClick;
            Panel.SpriteColor = Color.Blue;

            this.AttachChild(Panel);
        }

        private void RenameClick(MouseEvent e, Scene s)
        {
            if (e.ClickDownEdgeL)
            {
                Game1.renameForm.DefaultName = target.ClassID;
                Game1.renameForm.ShowDialog();
                target.ClassID = Game1.renameForm.ResultName;
            }
        }
    }

    public class ColorChangeButton : Scene 
    {
        Scene target;

        public ColorChangeButton(Scene ChangeTarget,int X,int Y) 
        {
            target = ChangeTarget;
            this.Position = new Vector2(X, Y);
            this.Touchable = true;


            TextSprite A = new TextSprite();
            A.Touchable = true;
            A.Text = "[ A ]";
            A.TouchEvent += AClick;
            A.SpriteColor = Color.Black;

            this.AttachChild(A);


            TextSprite Panel = new TextSprite();
            Panel.Touchable = true;
            Panel.Text = "[ Color Panel ]";
            Panel.TouchEvent += PanelClick;
            Panel.SpriteColor = Color.Blue;

            this.AttachChild(Panel);

            A.Position = new Vector2(-75, 0);
            Panel.Position = new Vector2(50, 0);
        }

        private void PanelClick(MouseEvent e, Scene s) 
        {
            if (e.ClickDownEdgeL) 
            {
                if (Game1.colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {

                    Game1.commandManager.Execute(new ColorCommand(target,new Color(Game1.colorDialog.Color.R, Game1.colorDialog.Color.G, Game1.colorDialog.Color.B, s.SpriteColor.A)));

                }
            }
        }

        private void RClick(MouseEvent e, Scene s)
        {
            if (e.WheelUp) 
            {
                Color color = target.SpriteColor;

                if (color.R < 5)
                    color.R = 0;
                else
                    color.R -= 5;

                target.SpriteColor = color;
            }
            else if (e.WheelDown) 
            {
                Color color = target.SpriteColor;

                if (color.R > 250)
                    color.R = 255;
                else
                    color.R += 5;

                target.SpriteColor = color;
            }        
        }
        private void GClick(MouseEvent e, Scene s)
        {
            if (e.WheelUp)
            {
                Color color = target.SpriteColor;

                if (color.G < 5)
                    color.G = 0;
                else
                    color.G -= 5;

                target.SpriteColor = color;
            }
            else if (e.WheelDown)
            {
                Color color = target.SpriteColor;

                if (color.G > 250)
                    color.G = 255;
                else
                    color.G += 5;

                target.SpriteColor = color;
            }
        }
        private void BClick(MouseEvent e, Scene s)
        {
            if (e.WheelUp)
            {
                Color color = target.SpriteColor;

                if (color.B < 5)
                    color.B = 0;
                else
                    color.B -= 5;

                target.SpriteColor = color;
            }
            else if (e.WheelDown)
            {
                Color color = target.SpriteColor;

                if (color.B > 250)
                    color.B = 255;
                else
                    color.B += 5;

                target.SpriteColor = color;
            }
        }
        private void AClick(MouseEvent e, Scene s)
        {
            if (e.WheelUp)
            {
                Color color = target.SpriteColor;

                if (color.A< 5)
                    color.A = 0;
                else
                    color.A -= 5;

                Game1.commandManager.Execute(new ColorCommand(target, color));
            }
            else if (e.WheelDown)
            {
                Color color = target.SpriteColor;

                if (color.A > 250)
                    color.A = 255;
                else
                    color.A += 5;


                Game1.commandManager.Execute(new ColorCommand(target,color));
            }
        }
    }


       

    public class DirectionSelectButton : TextSprite 
    {
        Declaration TargetItem;
        string Targetdirection;

        public DirectionSelectButton(Declaration targetItem, String direction)
            : base()
        {
            TargetItem = targetItem;
            Touchable = true;
            TouchEvent += Click;
            Targetdirection = direction;
            Text = "[ G: " + direction + " ]";
            SpriteColor = Color.Blue;
        }

        private void Click(MouseEvent e,Scene s)
        {
            if (e.ClickUpEdgeL)
            {
                TargetItem.Direction = Targetdirection;
            }
        }
    }
}
